﻿///// Buttons /////

using System;
using System.Collections.Generic;
using System.Collections;
//using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Buttons
{
    class Program
    {
        static void Main(string[] args)
        {
            // Demonstrates the different types of buttons.
            // 
            // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Buttons");
            Console.WriteLine("-> Creates a PDF file with widget annotations of type button;");
            Console.WriteLine("-> files generated:");
            Console.WriteLine("   buttons.pdf");

            // step 1: creation of a document-object
            Document document = new Document();
            try
            {
                // step 2:
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("buttons.pdf", FileMode.Create));
                // step 3:
                document.Open();
                writer.AddJavaScript("function showButtonState() {\n"
                        + "app.alert('Checkboxes:"
                        + " English: ' + this.getField('English').value + "
                        + "' French: ' + this.getField('French').value + "
                        + "' Dutch: ' + this.getField('Dutch').value + "
                        + "' Radioboxes: ' + this.getField('language').value);"
                        + "\n}");
                // step 4:
                PdfContentByte cb = writer.DirectContent;
                BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                String[] languages = { "English", "French", "Dutch" };
                Rectangle rect;

                PdfAppearance[] radiobuttonStates = new PdfAppearance[2];
                radiobuttonStates[0] = cb.CreateAppearance(20, 20);
                radiobuttonStates[0].Circle(10, 10, 9);
                radiobuttonStates[0].Stroke();
                radiobuttonStates[1] = cb.CreateAppearance(20, 20);
                radiobuttonStates[1].Circle(10, 10, 9);
                radiobuttonStates[1].Stroke();
                radiobuttonStates[1].Circle(10, 10, 3);
                radiobuttonStates[1].FillStroke();

                PdfFormField language = PdfFormField.CreateRadioButton(writer, true);
                language.FieldName = "language";
                language.ValueAsName = languages[0];

                for (int i = 0; i < languages.Length; i++)
                {
                    rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
                    addRadioButton(writer, rect, language, languages[i], radiobuttonStates, i == 0);
                    cb.BeginText();
                    cb.SetFontAndSize(bf, 18);
                    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0);
                    cb.EndText();
                }

                writer.AddAnnotation(language);
                PdfAppearance[] checkboxStates = new PdfAppearance[2];
                checkboxStates[0] = cb.CreateAppearance(20, 20);
                checkboxStates[0].Rectangle(1, 1, 18, 18);
                checkboxStates[0].Stroke();
                checkboxStates[1] = cb.CreateAppearance(20, 20);
                checkboxStates[1].SetRGBColorFill(255, 128, 128);
                checkboxStates[1].Rectangle(1, 1, 18, 18);
                checkboxStates[1].FillStroke();
                checkboxStates[1].MoveTo(1, 1);
                checkboxStates[1].LineTo(19, 19);
                checkboxStates[1].MoveTo(1, 19);
                checkboxStates[1].LineTo(19, 1);
                checkboxStates[1].Stroke();

                for (int i = 0; i < languages.Length; i++)
                {
                    rect = new Rectangle(260, 806 - i * 40, 280, 788 - i * 40);
                    createCheckbox(writer, rect, languages[i], checkboxStates);
                    cb.BeginText();
                    cb.SetFontAndSize(bf, 18);
                    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 290, 790 - i * 40, 0);
                    cb.EndText();
                }

                PdfAppearance normal = cb.CreateAppearance(100, 50);
                normal.SetColorFill(BaseColor.GRAY);
                normal.Rectangle(5, 5, 90, 40);
                normal.Fill();
                PdfAppearance rollover = cb.CreateAppearance(100, 50);
                rollover.SetColorFill(BaseColor.RED);
                rollover.Rectangle(5, 5, 90, 40);
                rollover.Fill();
                PdfAppearance down = cb.CreateAppearance(100, 50);
                down.SetColorFill(BaseColor.BLUE);
                down.Rectangle(5, 5, 90, 40);
                down.Fill();
                PdfFormField pushbutton = PdfFormField.CreatePushButton(writer);
                pushbutton.FieldName = "PushAction";
                pushbutton.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal);
                pushbutton.SetAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover);
                pushbutton.SetAppearance(PdfAnnotation.APPEARANCE_DOWN, down);
                pushbutton.SetWidget(new Rectangle(40, 650, 150, 680), PdfAnnotation.HIGHLIGHT_PUSH);
                pushbutton.Action = PdfAction.JavaScript("this.showButtonState()", writer);
                writer.AddAnnotation(pushbutton);
            }

            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
            }

            // step 5: we close the document
            document.Close();

            Console.ReadLine();
        }

        private static void addRadioButton(PdfWriter writer, Rectangle rect, PdfFormField radio, String name, PdfAppearance[] onOff, bool on)
        {
            PdfFormField field = PdfFormField.CreateEmpty(writer);
            field.SetWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT);

            if (on)
                field.AppearanceState = name;
            else
                field.AppearanceState = "Off";

            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, name, onOff[1]);
            radio.AddKid(field);
        }

        private static void createCheckbox(PdfWriter writer, Rectangle rect, String name, PdfAppearance[] onOff)
        {
            PdfFormField field = PdfFormField.CreateCheckBox(writer);
            field.SetWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT);
            field.FieldName = name;
            field.ValueAsName = "Off";
            field.AppearanceState = "Off";
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);
            writer.AddAnnotation(field);
        }
    }
}
